home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Color Picker SDK / Sample Code / ScrapPicker Sample / Sources ƒ / ScrapPickerDraw.c < prev    next >
Encoding:
Text File  |  1997-06-13  |  8.1 KB  |  329 lines  |  [TEXT/CWIE]

  1.  
  2. //===============================================================================
  3. //
  4. //                                  ScrapPickerDraw.c
  5. //
  6. //    Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  7. //  by john calhoun & David Hayward
  8. //
  9. //===============================================================================
  10.  
  11.  
  12. #include <Folders.h>            // <-- Public interfaces.
  13. #include <Resources.h>
  14. #include <TextUtils.h>
  15. #include "ScrapPicker.h"        // <-- Project interfaces.
  16. #include "PickerCommon.h"
  17.  
  18.  
  19. static void DrawColorList (PickerStorageHandle);
  20. static void DrawColorRects (PickerStorageHandle storage, Boolean drawOrig);
  21. static void DrawChangedProc (PickerStorageHandle storage);
  22. static pascal void DrawNewColorProc (SInt16, SInt16, GDHandle, SInt32);
  23.  
  24.  
  25. #if GENERATINGCFM
  26. RoutineDescriptor DrawNewColorProcRD = 
  27.         BUILD_ROUTINE_DESCRIPTOR (uppDeviceLoopDrawingProcInfo, DrawNewColorProc);
  28. #endif
  29.  
  30.  
  31. //=====================================================================  Functions
  32. //---------------------------------------------------------------------  DrawColorEditor
  33. // Draws all sliders and updates the edit text items.
  34.  
  35. void DrawColorEditor (PickerStorageHandle storage, UInt32 flags)
  36. {
  37.     PickerStoragePtr    pStorage;
  38.     
  39.     pStorage = *storage;
  40.     
  41.     if (!pStorage->visible)
  42.         return;
  43.     
  44.     if ((flags & kDrawOrig) && (flags & kDrawNew))
  45.         DrawColorList(storage);
  46.     else
  47.     {
  48.         if (flags & kDrawOrig)
  49.             DrawColorRects(storage,true);
  50.         if (flags & kDrawNew)
  51.             DrawColorRects(storage,false);
  52.     }
  53.     
  54.     if (flags & kDrawPict)
  55.     {
  56.         PicHandle    thePict;
  57.         OSErr        theErr;
  58.         SInt16        resFile;
  59.         
  60.                 // -1 and 0 for pictNumber means to use one of the two internal…
  61.                 // PICTs in this picker.  Positive number mean grab it from the…
  62.                 // scrapbook.
  63.         if (pStorage->pictNumber <= 0)
  64.         {
  65.                 // Open our resource fork.
  66.             resFile = OpenComponentResFile((Component)pStorage->myself);
  67.             require_action(resFile > 0, fail, theErr = cantLoadPicker;);
  68.         }
  69.         else
  70.         {
  71.             resFile = OpenScrapBookFile();
  72.             require_action(resFile > 0, fail, theErr = ResError(););
  73.         }
  74.         
  75.         if (pStorage->pictNumber < 0)
  76.             thePict = GetPicture(128);
  77.         else if (pStorage->pictNumber == 0)
  78.             thePict = GetPicture(129);
  79.         else
  80.             thePict = (PicHandle)Get1IndResource('PICT', pStorage->pictNumber);
  81.         
  82.         if (thePict)
  83.         {
  84.             Rect        bounds, pictBounds;
  85.             
  86.             GetItemRect(storage, iPicture, &bounds);
  87.             EraseRect(&bounds);
  88.             
  89.             HLock((Handle)thePict);
  90.             pictBounds = (*thePict)->picFrame;
  91.             HUnlock((Handle)thePict);
  92.             
  93.                 // Is picture smaller than item rect?
  94.             if ((pictBounds.right - pictBounds.left) < (bounds.right - bounds.left))
  95.             {
  96.                 SInt16        originalWidth;
  97.                 
  98.                 originalWidth = bounds.right - bounds.left;
  99.                 bounds.left += (originalWidth - (pictBounds.right - pictBounds.left)) / 2;
  100.                 bounds.right = bounds.left + (pictBounds.right - pictBounds.left);
  101.             }
  102.             
  103.             if ((pictBounds.bottom - pictBounds.top) < (bounds.bottom - bounds.top))
  104.             {
  105.                 SInt16        originalHeight;
  106.                 
  107.                 originalHeight = bounds.bottom - bounds.top;
  108.                 bounds.top += (originalHeight - (pictBounds.bottom - pictBounds.top)) / 2;
  109.                 bounds.bottom = bounds.top + (pictBounds.bottom - pictBounds.top);
  110.             }
  111.             
  112.             DrawPicture(thePict, &bounds);
  113.             ReleaseResource((Handle)thePict);
  114.         }
  115.         
  116.         if (pStorage->pictNumber <= 0)
  117.         {
  118.             theErr = CloseComponentResFile(resFile);
  119.             check(theErr == noErr);
  120.         }
  121.         else
  122.             CloseResFile(resFile);
  123.     }
  124.     
  125. fail:
  126.     
  127.     if (flags & kDrawProc)
  128.         DrawChangedProc(storage);
  129. }
  130.  
  131. //---------------------------------------------------------------------  OpenScrapBookFile
  132.  
  133. SInt16 OpenScrapBookFile (void)
  134. {
  135.     CInfoPBRec    theBlock;
  136.     CInfoPBRec    *theBlockPtr;
  137.     Str255        fileName;
  138.     SInt32        foundDirID;
  139.     OSErr        theErr;
  140.     SInt16        foundVRefNum;
  141.     SInt16        resFile, count;
  142.     
  143.     resFile = -1;
  144.     
  145.     theErr = FindFolder(kOnSystemDisk, kSystemFolderType, kDontCreateFolder, 
  146.             &foundVRefNum, &foundDirID);
  147.     require(theErr == noErr, fail);
  148.     
  149.     count = 1;
  150.     theBlockPtr = &theBlock;
  151.     theBlockPtr->dirInfo.ioCompletion = 0L;
  152.     theBlockPtr->dirInfo.ioVRefNum = foundVRefNum;
  153.     theBlockPtr->dirInfo.ioFVersNum = 0;
  154.     theBlockPtr->dirInfo.ioFDirIndex = count;
  155.     theBlockPtr->dirInfo.ioNamePtr = fileName;
  156.     theBlockPtr->dirInfo.ioDrDirID = foundDirID;
  157.     
  158.     do
  159.     {
  160.         theErr = PBGetCatInfoSync(theBlockPtr);
  161.         if (theErr == noErr)
  162.         {
  163.             if ((theBlockPtr->hFileInfo.ioFlFndrInfo.fdType == 'sbkt') || 
  164.                     (theBlockPtr->hFileInfo.ioFlFndrInfo.fdType == 'sbkt'))
  165.                 goto done;
  166.         }
  167.         
  168. nextFile:
  169.         
  170.         count++;
  171.         theBlockPtr->hFileInfo.ioFDirIndex = count;
  172.         theBlockPtr->hFileInfo.ioDirID = foundDirID;
  173.     }
  174.     while (theErr == noErr);
  175.     
  176.     if (theErr == fnfErr)
  177.         theErr = noErr;
  178.     else
  179.         goto fail;
  180.     
  181. done:
  182.     
  183.     resFile = HOpenResFile(foundVRefNum, foundDirID, fileName, fsCurPerm);
  184.     
  185. fail:
  186.     
  187.     return resFile;
  188. }
  189.  
  190. #pragma mark -------------------- Private Functions
  191. //=====================================================================  Private Functions
  192. //---------------------------------------------------------------------  DrawColorList
  193. // Draws the two (original and new) color rectangle frames in the top 
  194. // right of the picker.  Also, calls the function to fill them with color.
  195.  
  196. static void DrawColorList (PickerStorageHandle storage)
  197. {
  198.     Rect                oneRect;
  199.     PickerStoragePtr    pStorage;
  200.     PenState            oldPen;
  201.     
  202.     pStorage = *storage;
  203.     
  204.     if (!pStorage->visible)
  205.         return;
  206.     
  207.     GetPenState(&oldPen);
  208.     PenNormal();
  209.     ForeColor(blackColor);
  210.     
  211.     GetItemRect(storage, iOrigColor, &oneRect);
  212.     DropRectShadow(&oneRect, 5, (pStorage->depth > 4));
  213.     GetItemRect(storage, iNewColor, &oneRect);
  214.     DropRectShadow(&oneRect, 5, (pStorage->depth > 4));
  215.     
  216.     DrawColorRects(storage, true);
  217.     DrawColorRects(storage, false);
  218.     
  219.     ForeColor(blackColor);
  220.     SetPenState(&oldPen);
  221. }
  222.  
  223. //---------------------------------------------------------------------  DrawColorRects
  224. // Fills in the color rectangles (original and new) in the top right 
  225. // corner of the picker.  Will handle the nasty B&W case as well.
  226.  
  227. static void DrawColorRects (PickerStorageHandle storage, Boolean drawOrig)
  228. {
  229.     Rect                    theRect;
  230.     RgnHandle                newColorRgn;
  231.     PixPatHandle            hPattern;
  232.     PickerStoragePtr        pStorage;
  233.     DeviceLoopDrawingUPP    drawNewColorProcUPP;
  234.     
  235.     pStorage = *storage;
  236.     
  237. #if GENERATINGCFM
  238.     drawNewColorProcUPP = &DrawNewColorProcRD;
  239. #else
  240.     drawNewColorProcUPP = &DrawNewColorProc;
  241. #endif
  242.     
  243.     PenNormal();
  244.     
  245.     if (drawOrig)
  246.     {
  247.         GetItemRect(storage, iOrigColor, &theRect);
  248.         InsetRect(&theRect, 1, 1);
  249.         hPattern = pStorage->origColorPat;
  250.         
  251.         if (pStorage->useColorPats)
  252.         {
  253.                     // Draw the old color rect.
  254.             FillCRect(&theRect, hPattern);
  255.             check(QDError() == noErr);
  256.         }
  257.         else
  258.         {
  259.                     // Draw B&W old color
  260.             HLock((Handle)hPattern);
  261.             FillRect(&theRect, (ConstPatternParam)*hPattern);
  262.             HUnlock((Handle)hPattern);
  263.         }
  264.     }
  265.     else
  266.     {
  267.         GetItemRect(storage, iNewColor, &theRect);
  268.         InsetRect(&theRect, 1, 1);
  269.         hPattern = pStorage->newColorPat;
  270.         
  271.         SetColorPattern(storage, kNewColor);
  272.         if (pStorage->useColorPats)
  273.         {
  274.                     // Draw the new color rect.
  275.             newColorRgn = NewRgn();
  276.             if (newColorRgn)
  277.             {
  278.                 RectRgn(newColorRgn, &theRect);
  279.                 ClipRect(&theRect);
  280.                 DeviceLoop(newColorRgn, drawNewColorProcUPP, (long)storage, 0);
  281.                 DisposeRgn(newColorRgn);
  282.                 ClipRect(&pStorage->port->portRect);
  283.             }
  284.         }
  285.         else
  286.         {
  287.                     // Draw B&W new color
  288.             HLock((Handle)hPattern);
  289.             FillRect(&theRect, (ConstPatternParam)*hPattern);
  290.             HUnlock((Handle)hPattern);
  291.         }
  292.     }
  293. }
  294.  
  295. //---------------------------------------------------------------------  DrawChangedProc
  296.  
  297. static void DrawChangedProc (PickerStorageHandle storage)
  298. {
  299.     PickerStoragePtr    pStorage;
  300.     
  301.     pStorage = *storage;
  302.     
  303.                 // Call the app's color-changed procedure (if any).
  304.     if ((!pStorage->calledNPickColor) && pStorage->colorProc)
  305.     {
  306.         PMColor        color;
  307.         
  308.         (void) doPickerGetColor(storage, kNewColor, &color);
  309.         CallColorChangedProc((*storage)->colorProc, (*storage)->colorProcData, &color);
  310.     }
  311. }
  312.  
  313. //---------------------------------------------------------------------  DrawNewColorProc
  314.  
  315. static pascal void DrawNewColorProc (short depth, short deviceFlags, GDHandle targetDevice, 
  316.         SInt32 storage)
  317. {
  318. #pragma unused (deviceFlags, depth)
  319.     GDHandle            saveGD;
  320.     
  321.     saveGD = GetGDevice();
  322.     SetGDevice(targetDevice);
  323.     
  324.     FillCRect(&(((*(PickerStorageHandle)storage)->port)->portRect), (*(PickerStorageHandle)storage)->newColorPat);
  325.     
  326.     SetGDevice(saveGD);
  327. }
  328.  
  329.